﻿using System;
using System.ServiceModel;
using MBWebService.Core.Enums;
using System.IO;
using MBWebService.Core.Classes;
using System.ServiceModel.Web;
using System.Collections.Generic;
using Microsoft.Ajax.Samples;

namespace MBWebService.Core.Interfaces
{
    // NOTE: If you change the interface name "IMediaBrowserService" here, you must also update the reference to "IMediaBrowserService" in Web.config.
    [ServiceContract]
    [ServiceKnownType(typeof(MetadataItem))]
    [ServiceKnownType(typeof(BasicCollectionItem))]
    public interface IMediaBrowserService
    {
        /// <summary>
        /// Retrieves an item from MediaBrowser given an Id, this is either a BasicItemCollection or MetadataItem depending on its type
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [OperationContract]
        [JSONPBehavior(callback = "method")]
        [WebGet(UriTemplate = "/item?id={id}", ResponseFormat = WebMessageFormat.Json)]
        BasicItem GetItem(Guid id);

        /// <summary>
        /// Gets the initial folders from the MediaBrowser startup screen
        /// </summary>
        /// <returns></returns>
        [OperationContract]
        [JSONPBehavior(callback = "method")]
        [WebGet(UriTemplate = "/startup", ResponseFormat = WebMessageFormat.Json)]
        BasicCollectionItem GetStartUpFolders();

        /// <summary>
        /// Gets a collection/folder from the MediaBrowser library
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [OperationContract]
        [JSONPBehavior(callback = "method")]
        [WebGet(UriTemplate = "/collection?id={id}", ResponseFormat = WebMessageFormat.Json)]
        BasicCollectionItem GetCollection(Guid id);

        /// <summary>
        /// Retrieves an item from MediaBrowser given an Id, this is either a BasicItemCollection or MetadataItem depending on its type
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [OperationContract]
        [JSONPBehavior(callback = "method")]
        [WebGet(UriTemplate = "/metadata?id={id}", ResponseFormat = WebMessageFormat.Json)]
        MetadataItem GetMetadata(Guid id);

        /// <summary>
        /// lists possible stream items
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [OperationContract]
        [JSONPBehavior(callback = "method")]
        [WebGet(UriTemplate = "/streamItems?id={id}", ResponseFormat = WebMessageFormat.Json)]
        string getStreamItems(Guid id);

        /// <summary>
        /// Streams a specific item given its Id and filename
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [OperationContract]
        [JSONPBehavior(callback = "method")]
        [WebGet(UriTemplate = "/stream?id={id}&fname={fname}&url={url}", ResponseFormat = WebMessageFormat.Json)]
        bool StreamItem(Guid id, string fname, string url);

        /// <summary>
        /// Retrieves an image for a particular item
        /// </summary>
        /// <param name="id">Id of the MediaItem</param>
        /// <param name="imageType">The type of image</param>
        /// <param name="maxHeight">O or int.MaxValue to ignore</param>
        /// <param name="maxWidth">O or int.MaxValue to ignore</param>
        /// <returns></returns>
        [OperationContract]
        [JSONPBehavior(callback = "method")]
        [WebGet(UriTemplate = "/image?id={id}&imagetype={imageType}&maxheight={maxHeight}&maxwidth={maxWidth}", ResponseFormat = WebMessageFormat.Json)]
        PictureItem GetImage(Guid id, ImageTypes imageType, int maxHeight, int maxWidth);

        /// <summary>
        /// Gets a list of available trailers and their source
        /// </summary>
        /// <param name="id">The id of the library to get trailers for</param>
        /// <returns></returns>
        [OperationContract]
        [JSONPBehavior(callback = "method")]
        [WebGet(UriTemplate = "/trailers?id={id}", ResponseFormat = WebMessageFormat.Json)]
        TrailerInfo[] GetTrailers(Guid id);

        /// <summary>
        /// Gets a list of all devices connected to the service
        /// </summary>
        /// <returns></returns>
        [OperationContract]
        [JSONPBehavior(callback = "method")]
        [WebGet(UriTemplate = "/devices", ResponseFormat = WebMessageFormat.Json)]
        PlaybackDevice[] GetAttachedDevices();

        /// <summary>
        /// Gets a list of all recently added files
        /// </summary>
        /// <returns></returns>
        [OperationContract]
        [JSONPBehavior(callback = "method")]
        [WebGet(UriTemplate = "/recent?id={id}", ResponseFormat = WebMessageFormat.Json)]
        BasicCollectionItem GetRecentlyAdded(Guid id);

        /// <summary>
        /// Gets a list of all recently added files
        /// </summary>
        /// <returns></returns>
        [OperationContract]
        [JSONPBehavior(callback = "method")]
        [WebGet(UriTemplate = "/watched?id={id}", ResponseFormat = WebMessageFormat.Json)]
        BasicCollectionItem GetRecentlyWatched(Guid id);

        #region Client based queries

        /// <summary>
        /// Plays a specific item given its Id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [OperationContract]
        [WebGet(UriTemplate = "/play?id={id}&player={player}&queue={queue}", ResponseFormat = WebMessageFormat.Json)]
        [JSONPBehavior(callback = "method")]
        bool PlayItem(Guid id, string player, bool queue);

        /// <summary>
        /// Gets an object representing what is currently playing in Media Center
        /// </summary>
        /// <param name="waitForUpdate"></param>
        /// <returns></returns>
        [OperationContract]
        [WebGet(UriTemplate = "/nowplaying?wait={waitForUpdate}&player={player}", ResponseFormat = WebMessageFormat.Json)]
        [JSONPBehavior(callback = "method")]
        NowPlayingItem GetNowPlaying(bool waitForUpdate, string player);

        /// <summary>
        /// Sends a command for Media Center to perform
        /// </summary>
        /// <param name="command"></param>
        [OperationContract]
        [WebGet(UriTemplate = "/command?type={command}&player={player}")]
        [JSONPBehavior(callback = "method")]
        void SendCommand(CommandTypes command, string player);

        /// <summary>
        /// Seeks to a specific time (in seconds) for the currently playing item
        /// </summary>
        /// <param name="seconds">Time in seconds</param>
        [OperationContract]
        [WebGet(UriTemplate = "/seek?duration={seconds}&player={player}")]
        [JSONPBehavior(callback = "method")]
        void Seek(int seconds, string player);

        /// <summary>
        /// Tells media Center to go to a specific screen
        /// </summary>
        /// <param name="screenType">The screen to go to</param>
        /// <param name="extraInfo">Any extra information for the screen in question</param>
        [OperationContract]
        [WebGet(UriTemplate = "/screen?type={screenType}&params={extraInfo}&player={player}")]
        [JSONPBehavior(callback = "method")]
        void GoToScreen(ScreenType screenType, string extraInfo, string player);

        /// <summary>
        /// Plays a given library item on the host PC
        /// </summary>
        /// <param name="trailer"></param>
        [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "/playtrailer?player={player}", RequestFormat = WebMessageFormat.Json)]
        void PlayTrailer(TrailerInfo trailer, string player);

        #endregion
    }
}
